package ar.com.gep.soccer.services.dto;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.Expose;

import ar.com.cdt.framework.business.dto.BaseDTO;

public class RoundDTO extends BaseDTO {

	private static final long serialVersionUID = -8553261754351375087L;

	@Expose
	List<GameDTO> games = new ArrayList<GameDTO>();

	public List<GameDTO> getGames() {
		return games;
	}

	public void setGames(List<GameDTO> games) {
		this.games = games;
	}

	public void calculatePosition(List<PositionDTO> positions) {
		PositionDTO positionDTO;
		for (GameDTO gameDTO : getGames()) {
			if (gameDTO.hasWinner()) {
				// Winner
				positionDTO = getPosition(gameDTO.getWinner(), positions);
				positionDTO.setWin(positionDTO.getWin() + 1);
				// Loser
				positionDTO = getPosition(gameDTO.getLoser(), positions);
				positionDTO.setLose(positionDTO.getLose() + 1);
			} else {
				// Home
				positionDTO = getPosition(gameDTO.getAway(), positions);
				positionDTO.setDraw(positionDTO.getDraw() + 1);
				// Away
				positionDTO = getPosition(gameDTO.getHome(), positions);
				positionDTO.setDraw(positionDTO.getDraw() + 1);
			}
			// Away goals
			positionDTO = getPosition(gameDTO.getAway(), positions);
			positionDTO.setGoal(positionDTO.getGoal() + gameDTO.getGoalAway());
			positionDTO.setGoalOwn(positionDTO.getGoalOwn() + gameDTO.getGoalHome());
			// home goals
			positionDTO = getPosition(gameDTO.getHome(), positions);
			positionDTO.setGoal(positionDTO.getGoal() + gameDTO.getGoalHome());
			positionDTO.setGoalOwn(positionDTO.getGoalOwn() + gameDTO.getGoalAway());
		}
	}

	private PositionDTO getPosition(TeamDTO winner, List<PositionDTO> positions) {
		for (PositionDTO positionDTO : positions) {
			if (positionDTO.getTeam().equals(winner)) {
				return positionDTO;
			}
		}
		return null;
	}

}
